﻿Shader "Custom/RimLighting" {
    Properties {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _BumpMap ("Bumpmap", 2D) = "bump" {}
        _OcclusionStrength("Strength", Range(0.0, 1.0)) = 1.0
        _OcclusionMap("Occlusion", 2D) = "white" {}
        //边缘光颜色
        _RimColor("Rim Color",Color) =(1,1,1,1)
        //边缘光强度
        _RimPower("Rim Power", Range(0.5,8.0)) = 3.0
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200
        
        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0
        //#include "UnityStandardInput.cginc"


        sampler2D _MainTex;

        struct Input {
            float2 uv_MainTex;

            float2 uv_BumpMap;

            float2 uv_OcclusionMap;
            //法线
            float3 worldNormal;
            //视角方向
            float3 viewDir;
        };

        sampler2D _BumpMap;
        fixed4 _Color;
        fixed4 _RimColor;
        half _RimPower;
        half _OcclusionStrength;
        sampler2D _OcclusionMap;
        half Occlusion(float2 uv)
        {
         #if (SHADER_TARGET < 30)
         // SM20: instruction count limitation
         // SM20: simpler occlusion
          return tex2D(_OcclusionMap, uv).g;
           #else
             half occ = tex2D(_OcclusionMap, uv).g;
           return LerpOneTo (occ, _OcclusionStrength);
          #endif
         }
        void surf (Input IN, inout SurfaceOutputStandard o) {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
            o.Alpha = c.a;
            o.Normal = UnpackNormal(tex2D (_BumpMap, IN.uv_BumpMap)); //法线贴图计算
            //half oc=Occlusion(IN.uv_OcclusionMap); //遮挡贴图计算
            //o.Albedo = c.rgb * oc ; 
            o.Occlusion = Occlusion(IN.uv_OcclusionMap);
            half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal));
            //half rim = 1.0 - saturate(dot(normalize(IN.viewDir), IN.worldNormal));
            o.Emission = _RimColor.rgb * pow(rim, _RimPower);
        }

        ENDCG
    }
    FallBack "Diffuse"
}
